<template>
  <div class="container el-card__body">
    <el-card class="header">
      <el-row>
        <el-col>{{ id ? '试题修改' : '试题录入' }}</el-col>
      </el-row>
    </el-card>
    <el-card>
      <el-form
        label-width="100px"
        class="demo-ruleForm"
        :rules="addFormRules"
        :model="addForm"
        ref="addFormRef"
      >
        <el-form-item label="学科:" prop="subjectID">
          <el-select
            placeholder="请选择"
            v-model="addForm.subjectID"
            @change="change1"
          >
            <el-option
              v-for="item in options1"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录:" prop="catalogID">
          <el-select placeholder="请选择" v-model="addForm.catalogID">
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业:" prop="enterpriseID">
          <el-select placeholder="请选择" v-model="addForm.enterpriseID">
            <el-option
              v-for="(item, index) in options3"
              :key="index"
              :label="item.company"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 城市 -->
        <el-form-item label="城市" prop="province">
          <el-select
            v-model="addForm.province"
            placeholder="请选择"
            @change="change2"
          >
            <el-option
              v-for="(item, index) in options8"
              :key="index"
              :label="item"
              :value="index"
            >
            </el-option>
          </el-select>
          <el-select v-model="addForm.city" placeholder="请选择" prop="city">
            <el-option
              v-for="(item, index) in options9"
              :key="index"
              :label="item"
              :value="index"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 方向 -->
        <el-form-item label="方向:" prop="direction">
          <el-select placeholder="请选择" v-model="addForm.direction">
            <el-option
              v-for="item in options6"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题型:" prop="questionType">
          <el-radio v-model="addForm.questionType" @change="change3" label="1"
            >单选</el-radio
          >
          <el-radio v-model="addForm.questionType" @change="change3" label="2"
            >多选</el-radio
          >
          <el-radio v-model="addForm.questionType" @change="change3" label="3"
            >简答</el-radio
          >
        </el-form-item>
        <el-form-item label="难度:" prop="difficulty">
          <el-radio v-model="addForm.difficulty" label="1">简单</el-radio>
          <el-radio v-model="addForm.difficulty" label="2">一般</el-radio>
          <el-radio v-model="addForm.difficulty" label="3">困难</el-radio>
        </el-form-item>
        <el-form-item label="题干:" prop="question" style="height:250px">
          <quill-editor
            v-model="addForm.question"
            style="height:200px"
          ></quill-editor>
        </el-form-item>
        <div class="choose" v-show="isChooseShow">
          <el-form-item label="选项:" prop="name">
            <!-- 单选 -->
            <el-row v-if="isDisabled">
              <el-radio
                v-model="addForm.options"
                label="1"
                style="padding-bottom:50px"
                >A:<el-input></el-input
                ><el-upload style="display:inline-block">
                  <div class="upload-Img">上传图片</div>
                  <i class="el-icon-circle-close icon"></i>
                </el-upload>
              </el-radio>

              <br />
              <el-radio
                v-model="addForm.options"
                label="2"
                style="padding-bottom:50px"
                >B:<el-input></el-input>
                <el-upload style="display:inline-block">
                  <div class="upload-Img">上传图片</div>
                  <i class="el-icon-circle-close icon"></i>
                </el-upload> </el-radio
              ><br />
              <el-radio
                v-model="addForm.options"
                label="3"
                style="padding-bottom:50px"
                >C:<el-input></el-input>
                <el-upload style="display:inline-block">
                  <div class="upload-Img">上传图片</div>
                  <i class="el-icon-circle-close icon"></i>
                </el-upload> </el-radio
              ><br />
              <el-radio
                v-model="addForm.options"
                label="4"
                style="padding-bottom:50px"
                >D:<el-input></el-input>
                <el-upload style="display:inline-block">
                  <div class="upload-Img">上传图片</div>
                  <i class="el-icon-circle-close icon"></i>
                </el-upload> </el-radio
              ><br />
            </el-row>
            <!-- 多选 -->
            <el-row v-if="!isDisabled">
              <el-checkbox-group v-model="addForm.options">
                <el-checkbox style="padding-bottom:50px" label="A"
                  >A: <el-input style="width:331.5px"></el-input
                  ><el-upload style="display:inline-block">
                    <div class="upload-Img1">上传图片</div>
                    <i
                      class="el-icon-circle-close icon1"
                    ></i> </el-upload></el-checkbox
                ><br />
                <el-checkbox style="padding-bottom:50px" label="B"
                  >B: <el-input style="width:331.5px"></el-input
                  ><el-upload style="display:inline-block">
                    <div class="upload-Img1">上传图片</div>
                    <i
                      class="el-icon-circle-close icon1"
                    ></i> </el-upload></el-checkbox
                ><br />
                <el-checkbox style="padding-bottom:50px" label="C"
                  >C: <el-input style="width:331.5px"></el-input
                  ><el-upload style="display:inline-block">
                    <div class="upload-Img1">上传图片</div>
                    <i
                      class="el-icon-circle-close icon1"
                    ></i> </el-upload></el-checkbox
                ><br />
                <el-checkbox style="padding-bottom:50px" label="D"
                  >D: <el-input style="width:331.5px"></el-input
                  ><el-upload style="display:inline-block">
                    <div class="upload-Img1">上传图片</div>
                    <i
                      class="el-icon-circle-close icon1"
                    ></i> </el-upload></el-checkbox
                ><br />
              </el-checkbox-group>
            </el-row>
            <el-row
              :key="index"
              v-for="(domain, index) in dynamicValidateForm.domains"
            >
              <el-checkbox
                v-if="!isDisabled"
                v-model="addForm.options"
                :label="domainList[index + 4]"
                style="padding-bottom:50px"
              >
                {{ domainList[index + 4] }}
                <el-input style="width:331.5px"></el-input>
                <el-upload style="display:inline-block">
                  <div class="upload-Img1">上传图片</div>
                  <i class="el-icon-circle-close icon1"></i>
                </el-upload> </el-checkbox
            ></el-row>
            <el-button
              type="danger"
              @click="addDomain"
              class="btnAdd"
              :disabled="isDisabled"
              >+增加选项和答案</el-button
            >
          </el-form-item>
        </div>
        <el-form-item label="解析视频:" prop="name">
          <el-input style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="答案解析:" prop="answer">
          <quill-editor
            v-model="addForm.answer"
            style="height:200px"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="题目备注:" prop="name" class="timu">
          <el-input type="textarea" v-model="addForm.remarks" :rows="2">
          </el-input>
        </el-form-item>
        <el-form-item label="试题标签:" prop="name">
          <el-select
            v-model="addForm.tags"
            placeholder="请选择"
            style="width:25%"
          >
            <el-option
              v-for="item in options5"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option> </el-select
          ><br />
          <el-button class="btn1" :type="id ? 'success' : 'primary'" @click="addQuestions">{{
            id ? '确认修改' : '确认提交'
          }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { simple as subjectsSimple } from '@/api/hmmm/subjects.js'
import { simple as directorysSimple } from '@/api/hmmm/directorys.js'
import { list as companysList } from '@/api/hmmm/companys.js'
import { provinces, citys } from '@/api/hmmm/citys'
import { add as questionsAdd, detail, update } from '@/api/hmmm/questions.js'
import { simple as tagsSimple } from '@/api/hmmm/tags.js'
export default {
  data() {
    return {
      dynamicValidateForm: {
        domains: [
          {
            value: ''
          }
        ]
      },
      domainList: {
        4: 'E:',
        5: 'F:',
        6: 'G:',
        7: 'H:',
        8: 'I:',
        9: 'J:',
        10: 'K:'
      },
      addForm: {
        subjectID: '',
        catalogID: '',
        enterpriseID: '',
        province: '',
        city: '',
        direction: '',
        questionType: '1',
        difficulty: '1',
        question: '',
        answer: '',
        options: [],
        tags: '',
        remarks: ''
      },
      addFormRules: {
        subjectID: [{ required: true, message: '请选择学科', trigger: 'blur' }],
        catalogID: [{ required: true, message: '请选择目录', trigger: 'blur' }],
        enterpriseID: [
          { required: true, message: '请选择企业', trigger: 'blur' }
        ],
        province: [{ required: true, message: '请选择城市', trigger: 'blur' }],
        direction: [{ required: true, message: '请选择方向', trigger: 'blur' }],
        questionType: [
          { required: true, message: '请选择题型', trigger: 'blur' }
        ],
        difficulty: [
          { required: true, message: '请选择难度', trigger: 'blur' }
        ],
        question: [{ required: true, message: '请填写题干', trigger: 'blur' }],
        answer: [{ required: true, message: '请填写答案解析', trigger: 'blur' }]
      },
      options1: [],
      options2: [],
      options3: [],
      options6: [
        {
          value: 1,
          label: 'o2o'
        },
        {
          value: 2,
          label: '外包服务'
        },
        {
          value: 3,
          label: '企业服务'
        },
        {
          value: 4,
          label: '互联网金融'
        },
        {
          value: 5,
          label: '企业咨询'
        },
        {
          value: 6,
          label: '互联网'
        },
        {
          value: 7,
          label: '电子商务'
        },
        {
          value: 8,
          label: '其他'
        }
      ],

      options8: provinces(),
      options9: [],

      isDisabled: true,
      isChooseShow: true,
      id: '',
      options5: []
    }
  },
  created() {
    this.subjectLists()
    this.companysSimple()
    if (this.$route.query.id) {
      this.id = parseInt(this.$route.query.id)
      this.getEditList()
      this.change1()
    }
  },
  methods: {
    async subjectLists() {
      try {
        const { data: data1 } = await subjectsSimple()
        this.options1 = data1
        const { data: data2 } = await tagsSimple({ id: this.value1 })
        this.options5 = data2
      } catch (err) {}
    },
    async change1() {
      try {
        const { data } = await directorysSimple(this.addForm.subjectID)
        this.options2 = data
      } catch (err) {}
    },
    async companysSimple() {
      try {
        const { data } = await companysList(this.addForm.enterpriseID)
        console.log(data)
        this.options3 = data.items
      } catch (err) {}
    },

    // 选省获取到市
    change2() {
      this.options9 = citys(this.options8[this.addForm.province])
    },
    change3() {
      console.log(this.addForm.questionType)
      if (this.addForm.questionType === '2') {
        this.isDisabled = false
        this.isChooseShow = true
      } else if (this.addForm.questionType === '3') {
        this.isChooseShow = false
      } else {
        this.isDisabled = true
        this.isChooseShow = true
      }
    },
    addQuestions() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        if (this.id) {
          await update({
            id: this.id,
            subjectID: this.addForm.subjectID,
            catalogID: this.addForm.catalogID,
            enterpriseID: this.addForm.enterpriseID,
            province: this.addForm.province,
            city: this.addForm.city,
            direction: this.addForm.direction,
            questionType: this.addForm.questionType,
            difficulty: this.addForm.difficulty,
            question: this.addForm.question,
            options: [],
            answer: this.addForm.answer,
            tags: this.addForm.tags
              ? this.options5[this.addForm.tags].tagName
              : null
          })
          this.$message.success('修改成功')
          this.$router.back()
        } else {
          try {
            await questionsAdd({
              subjectID: this.addForm.subjectID,
              catalogID: this.addForm.catalogID,
              enterpriseID: this.addForm.enterpriseID,
              province: this.options8[this.addForm.province],
              city: this.options9[this.addForm.city],
              direction: this.options6[this.addForm.direction].label,
              questionType: this.addForm.questionType,
              difficulty: this.addForm.difficulty,
              question: this.addForm.question,
              options: [],
              answer: this.addForm.answer
            })
            this.$message.success('提交成功')
            this.$router.push('/questions/list')
          } catch (err) {
            this.$message.error('提交失败')
          }
        }
      })
    },
    async getEditList() {
      try {
        const { data } = await detail({ id: this.id })
        this.addForm = data
      } catch (err) {
        this.$message.error('获取数据失败')
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        value: ''
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.header {
  border-bottom: 1px solid #e3e3e3;
}
.timu {
  width: 400px;
  padding-top: 50px;
}
.choose {
  padding-top: 50px;
}
.upload-Img {
  border: 1px dashed #d9d9d9;
  text-align: center;
  height: 60px;
  line-height: 60px;
  margin-left: 5px;
  width: 100px;
  border-radius: 5px;
}
.el-upload {
  position: relative;
}
.icon {
  position: absolute;
  top: -7px;
  left: 485px;
  font-size: 20px;
  color: #d9d9d9;
}
.upload-Img1 {
  border: 1px dashed #d9d9d9;
  text-align: center;
  height: 60px;
  line-height: 60px;
  margin-left: 5px;
  width: 100px;
  border-radius: 5px;
}
.icon1 {
  position: absolute;
  top: -7px;
  left: 465px;
  font-size: 20px;
  color: #d9d9d9;
}
.btn1 {
  margin-top: 25px;
}
</style>
